package com.zelin.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.zelin.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.zelin.gulimall.product.entity.AttrEntity;
import com.zelin.gulimall.product.service.AttrAttrgroupRelationService;
import com.zelin.gulimall.product.service.AttrService;
import com.zelin.gulimall.product.service.CategoryService;
import com.zelin.gulimall.product.vo.AttrAttrGroupVo;
import com.zelin.gulimall.product.vo.AttrGroupAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zelin.gulimall.product.entity.AttrGroupEntity;
import com.zelin.gulimall.product.service.AttrGroupService;
import com.zelin.common.utils.PageUtils;
import com.zelin.common.utils.R;



/**
 * 属性分组
 *
 * @author wangfeng
 * @email wangfeng@gmail.com
 * @date 2020-12-05 11:33:07
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService relationService;

    //获取分类下所有分组&关联属性
    @GetMapping("/{catelogId}/withattr")
    public R withAttr(@PathVariable Long catelogId){
        //1.根据分类id查询分组及其属性信息
        List<AttrGroupAttrVo> attrGroupAttrVos = attrGroupService.findAttrGroupAttrByCategoryId(catelogId);
        //2.返回
        return R.ok().put("data",attrGroupAttrVos);
    }

    //添加属性与分组关联关系
    @PostMapping("attr/relation")
    public R save(@RequestBody List<AttrAttrGroupVo> vo){
        relationService.add(vo);
        return R.ok();
    }
    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") long catelogId){
        //PageUtils page = attrGroupService.queryPage(params);

        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] catelogPath = categoryService.findParentIdByCategoryId(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    //获取属性分组的关联的所有属性：/product/attrgroup/{attrgroupId}/attr/relation
    @GetMapping("{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable long attrgroupId){
        //1.根据分组id查询属性列表
        List<AttrEntity> attrEntities = attrService.relationAttr(attrgroupId);

        return R.ok().put("data",attrEntities);
    }

    //获取属性分组没有关联的其他属性
    @GetMapping("{attrgroupId}/noattr/relation")
    public R noAttrRelation(@RequestParam Map<String, Object> params,@PathVariable long attrgroupId){
        //1.获取属性分组没有关联的其它属性
        PageUtils pageUtils = attrService.relationNoAttr(params,attrgroupId);
        //2.返回
        return R.ok().put("page",pageUtils);

    }

    // 删除属性与分组的关联关系: attr/relation/delete
    @PostMapping("attr/relation/delete")
    public R relationDelete(@RequestBody AttrAttrGroupVo[] vo){
        relationService.relationDelete(vo);
        return R.ok();
    }
}
